//
//  CameraAlbum.m
//  IOS_FrameWork
//
//  Created by tan_xin on 14-7-1.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "CameraAlbum.h"
#import "AppDelegate.h"

static CameraAlbum *_cameraAlbum = nil;
@implementation CameraAlbum
@synthesize viewController;
@synthesize delegate;
+(CameraAlbum*)instance{
    @synchronized(self){
        if (_cameraAlbum==nil) {
            _cameraAlbum = [[self alloc] init];
        }
    }
    return _cameraAlbum;
}
-(id)init{
    if(self=[super init]){
        
    }
    return self;
}

//打开手机相机
-(void)openCameraInTargetViewController:(id)viewCtrl{
    self.viewController = viewCtrl;
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.view.frame = CGRectMake(0, 20, 320, iPhone5?568:480);

    self.picker.delegate = self;
    self.picker.allowsEditing = YES;
    self.picker.sourceType = sourceType;
    
    [viewController presentViewController:self.picker animated:YES completion:^{}];
}

//打开手机相册
-(void)openPhotoLibraryInTargetViewController:(id)viewCtrl{
    self.viewController = viewCtrl;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.view.frame = CGRectMake(0, 20, 320, iPhone5?568:480);
    self.picker.delegate = self;
    self.picker.allowsEditing = YES;
    self.picker.sourceType = sourceType;
    [viewController presentViewController:self.picker animated:YES completion:^{}];
}

-(void)openMutiblePhotoLibraryInTargetViewController:(id)viewCtrl{
    self.viewController = viewCtrl;
    ELCImagePickerController *elcPicker = [[ELCImagePickerController alloc] initImagePicker];
    elcPicker.maximumImagesCount = self.maximumImagesCount;
    elcPicker.returnsOriginalImage = NO; //Only return the fullScreenImage, not the fullResolutionImage
	elcPicker.imagePickerDelegate = self;
    [viewController presentViewController:elcPicker animated:YES completion:^{}];
}

- (void)saveImage:(UIImage *)image {
    //保存相片到相册
    //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    
    if (delegate && [delegate respondsToSelector:@selector(getSavedImage:)]) {
        [delegate getSavedImage:image];
    }
}

- (void)finishedPickImageWihtURL:(NSString *)imgURL {
    if (delegate && [delegate respondsToSelector:@selector(didFinishedPickImageWithURL:)]) {
        [delegate didFinishedPickImageWithURL:imgURL];
    }
}

- (void)saveImages:(NSMutableArray *)images{
    //保存相片到相册,多图
    //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [delegate getSavedImages:images];
}
#pragma Delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    NSLog(@"******info:%@",info);
    self.image = [info objectForKey:UIImagePickerControllerEditedImage];
    //当照片不可以编辑时，则保存原始拍照图片
    if (self.image==nil) {
        self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    
//    NSURL *url = [info objectForKeyOrNil:UIImagePickerControllerReferenceURL];
//    NSString *imgURL = [url description];
    
    [self performSelector:@selector(saveImage:)
               withObject:self.image
               afterDelay:0.5];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark ELCImagePickerControllerDelegate Methods

- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
    
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:[info count]];
	
	for (NSDictionary *dict in info) {
        
        UIImage *image = [dict objectForKey:UIImagePickerControllerOriginalImage];
        [images addObject:image];
	}
    [self performSelector:@selector(saveImages:)
               withObject:images
               afterDelay:0.5];
}

- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}
@end
